package com.ruoyi.common.utils;

import com.alibaba.excel.EasyExcel;

import com.ruoyi.common.core.domain.FileDoc;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DownExcel {

    public static void download(HttpServletResponse response, Class t, List list,String excelName) throws IOException {

        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode(excelName, "utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel
                .write(response.getOutputStream(), t).sheet("表1")
                .registerWriteHandler(new CustomCellWriteUtil())
                .doWrite(list); //用io流来写入数据
    }
    public static void downloadByHead(HttpServletResponse response, List<List<String>> head, List list,String excelName) throws IOException {

        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode(excelName, "utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel
                .write(response.getOutputStream()).head(head).sheet("表1")
                .registerWriteHandler(new CustomCellWriteUtil())
                .doWrite(list); //用io流来写入数据
    }

    public static void download(HttpServletResponse response,List<List<String>> head,List<List<String>> data,String excelName) throws Exception {
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode(excelName, "utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel
                .write(response.getOutputStream()).head(head).sheet("表1")
                .registerWriteHandler(new CustomCellWriteUtil())
                .doWrite(data);
    }


    public FileDoc fileDown(String url, HttpServletResponse response, HttpServletRequest req) {

        ServletOutputStream out = null;
        FileInputStream ips = null;
        FileDoc fileDoc = new FileDoc();
        try {
            //获取文件存放的路径
            File file = new File(url);

            //获取文件后缀
            String extension = file.getName().substring(file.getName().lastIndexOf("."));
//                String fileName="accessories"+extension;
            String fileName=file.getName();
            if(!file.exists()) {
                //如果文件不存在就跳出
                fileDoc.setMsg("文件不存在");
                fileDoc.setSuccess(false);
                return fileDoc;
            }
            ips = new FileInputStream(file);
            response.setContentType("multipart/form-data");
            response.addHeader("Content-Disposition", "attachment;filename=\"" + new String(file.getName().getBytes("utf-8"), "ISO8859-1")+ "\"");
            out = response.getOutputStream();
            //读取文件流
            int len = 0;
            byte[] buffer = new byte[1024 * 10];
            while ((len = ips.read(buffer)) != -1){
                out.write(buffer,0,len);
            }
            out.flush();
            fileDoc.setSuccess(true);
            fileDoc.setFileName(fileName);
            fileDoc.setMsg("下载成功");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
//                try {
//                    out.close();
//                    ips.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
        }
        return fileDoc;
    }

    // Bean --> Map 1: 利用Introspector和PropertyDescriptor 将Bean --> Map
    public static Map<String, Object> transBean2Map(Object obj) {
        if (obj == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                // 过滤class属性
                if (!key.equals("class")) {
                    // 得到property对应的getter方法
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);

                    map.put(key, value);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;

    }

}
